package com.jsu.clubmanagementsystem.controller.review;

import com.jsu.clubmanagementsystem.common.BaseResponse;
import com.jsu.clubmanagementsystem.common.ErrorCode;
import com.jsu.clubmanagementsystem.common.ResultUtils;
import com.jsu.clubmanagementsystem.exception.BusinessException;
import com.jsu.clubmanagementsystem.model.dto.review.ReviewConsequence;
import com.jsu.clubmanagementsystem.model.entity.Review;
import com.jsu.clubmanagementsystem.model.entity.User;
import com.jsu.clubmanagementsystem.model.enums.ReviewResultEnum;
import com.jsu.clubmanagementsystem.model.vo.review.BatchReviewConsequence;
import com.jsu.clubmanagementsystem.service.review.ReviewService;
import com.jsu.clubmanagementsystem.service.user.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/review")
public class ReviewController {

    @Resource
    private ReviewService reviewService;

    @Resource
    private UserService userService;

    /**
     * 审核申请
     *
     * @param reviewConsequence
     * @param request
     * @return
     */
    @PostMapping("/do")
    public BaseResponse<Long> doReview(@RequestBody ReviewConsequence reviewConsequence, HttpServletRequest request) {
        if (reviewConsequence == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "参数为空");
        }
        Integer result = reviewConsequence.getResult();
        ReviewResultEnum reviewResult = ReviewResultEnum.getByCode(result);
        if (reviewResult == null || reviewResult == ReviewResultEnum.WAITING) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "审核结果为空");
        }

        // 验证审核者身份
        reviewService.reviewerValidation(reviewConsequence, request);

        Review review = reviewService.getReviewByApplication(reviewConsequence.getApplicationId(),
                reviewConsequence.getType());
        if (review != null){
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "该申请已被审核");
        }

        // 处理审核
        Long reviewId = null;
        if (reviewResult == ReviewResultEnum.ACCEPT)
            reviewId = reviewService.acceptReview(reviewConsequence);
        else if (reviewResult == ReviewResultEnum.REJECT) {
            reviewId = reviewService.rejectReview(reviewConsequence);
        }
        return ResultUtils.success(reviewId);
    }

    /**
     * 批量审核
     *
     * @param reviewConsequences
     * @param request
     * @return
     */
    @PostMapping("/do-batch")
    public BaseResponse<BatchReviewConsequence> doBatchReview(@RequestBody List<ReviewConsequence> reviewConsequences, HttpServletRequest request) {
        if (reviewConsequences == null || reviewConsequences.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "参数为空");
        }
        for (ReviewConsequence consequence : reviewConsequences) {
            Integer result = consequence.getResult();
            ReviewResultEnum reviewResult = ReviewResultEnum.getByCode(result);
            if (reviewResult == null || reviewResult == ReviewResultEnum.WAITING) {
                throw new BusinessException(ErrorCode.PARAM_ERROR, "审核结果为空");
            }
            reviewService.reviewerValidation(consequence, request);
        }
        return ResultUtils.success(reviewService.processBatchReview(reviewConsequences));
    }

}
